//
//  XhlProgressHUD.m
//  XhlProgressHUDDemo
//
//  Created by rogue on 2019/1/7.
//  Copyright © 2019 rogue. All rights reserved.
//

#import "XhlProgressHUD.h"
#import <MBProgressHUD/MBProgressHUD.h>
NS_ASSUME_NONNULL_BEGIN
static NSInteger x_hud_viewController_tag_value = 199343;
static NSInteger x_hud_window_tag_value = 199344;

#pragma mark - ShowMessage
/**

 */
extern void xhl_showMessage(NSString *msg){
    [XhlProgressHUD showMessageOnWindow:msg];
}

/**

 */
extern void xhl_showMessageWithDuration(NSString *msg,NSTimeInterval duration){
    [XhlProgressHUD showMessageOnWindow:msg delay:duration];
}

/**

 */
extern void xhl_showProgress(NSString *msg,float progress){
    [XhlProgressHUD showProgressOnWindowTip:msg schedule:progress];
}

/**
 
 */
extern void xhl_hudLoading(void){
    [XhlProgressHUD showLoadingOnWindowTip:@""];
}

extern void xhl_hudLoadingMsg(NSString *msg){
    [XhlProgressHUD showLoadingOnWindowTip:msg];
}

/**
 
 */
extern void xhl_hudDismiss(void){
    [XhlProgressHUD hideLoadingOnWindow];
}

@implementation XhlProgressHUD

#pragma mark - UIViewController
+ (void)showLoading:(UIViewController *)viewController {
    [self showLoadingComplete:viewController hideComplete:^{}];
}

+ (void)showLoadingComplete:(UIViewController *)viewController hideComplete:(void (^)(void))hideComplete {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [viewController.view viewWithTag:x_hud_viewController_tag_value];
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
            hud.tag = x_hud_viewController_tag_value;
        }
        hud.completionBlock = hideComplete;
    });
}

+ (void)showTips:(UIViewController *)viewController tip:(NSString *)tip{
    [self showTips:viewController tip:tip delay:1.0f];
}

+ (void)showTips:(UIViewController *)viewController tip:(NSString *)tip delay:(float)delay {
    [self showTipsComplete:viewController tip:tip delay:delay hideComplete:^{}];
}

+ (void)showTipsComplete:(UIViewController *)viewController tip:(NSString *)tip delay:(float)delay hideComplete:(void (^)(void))hideComplete {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [viewController.view viewWithTag:x_hud_viewController_tag_value];
        if (![tip isKindOfClass:[NSString class]]) {
            [hud hideAnimated:true afterDelay:0];
            return;
        }
        if(tip.length < 1){
            [hud hideAnimated:true afterDelay:0];
            return;
        }
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:true];
            hud.userInteractionEnabled = NO;
            hud.tag = x_hud_viewController_tag_value;
            [hud showAnimated:true];
            
            hud.label.numberOfLines = 6;
        }
        hud.completionBlock = hideComplete;
        hud.mode = MBProgressHUDModeText;
        hud.label.text = tip;
        [hud hideAnimated:true afterDelay:delay];
    });
}

+ (void)showProgress:(UIViewController *)viewController tip:(NSString *)tip schedule:(CGFloat)schedule{
    tip = tip ? tip : @"";
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [viewController.view viewWithTag:x_hud_window_tag_value];
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:true];
            hud.tag = x_hud_viewController_tag_value;
            hud.mode = MBProgressHUDModeAnnularDeterminate;
            hud.label.numberOfLines = 6;
            [hud showAnimated:true];
        }
        hud.progress = schedule;
        hud.label.text = tip;
    });
}


#pragma mark - Window
+ (void)showMessageOnWindow:(NSString *)tip{
    [self showMessageOnWindow:tip delay:1.0f];
}


+ (void)showProgressOnWindowTip:(NSString *)tip schedule:(CGFloat)schedule{
    tip = tip ? tip : @"";
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
        MBProgressHUD *hud = [window viewWithTag:x_hud_window_tag_value];
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:window animated:true];
            hud.tag = x_hud_window_tag_value;
            hud.mode = MBProgressHUDModeAnnularDeterminate;
            hud.label.numberOfLines = 6;
            [hud showAnimated:true];
        }
        hud.progress = schedule;
        hud.label.text = tip;
    });
}

+ (void)showLoadingOnWindow{
    [self showLoadingOnWindowTip:@""];
}

+ (void)showLoadingOnWindowTip:(NSString *)tip{
    tip = tip ? tip : @"";
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
        
        MBProgressHUD *hud = [window viewWithTag:x_hud_window_tag_value];
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
            hud.label.numberOfLines = 6;
            hud.tag = x_hud_window_tag_value;
        }
        hud.label.text = tip;
    });
}

+ (void)showMessageOnWindow:(NSString *)tip delay:(float)delay {
    [self showMessageOnWindowComplete:tip delay:delay hideComplete:^{}];
}

+ (void)showMessageOnWindowComplete:(NSString *)tip delay:(float)delay hideComplete:(void (^)(void))hideComplete {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
        MBProgressHUD *hud = [window viewWithTag:x_hud_window_tag_value];
        if (![tip isKindOfClass:[NSString class]]) {
            [hud hideAnimated:true afterDelay:0];
            return;
        }
        if(tip.length < 1){
            [hud hideAnimated:true afterDelay:0];
            return;
        }
        if(!hud) {
            hud = [MBProgressHUD showHUDAddedTo:window animated:true];
            hud.userInteractionEnabled = NO;
            hud.tag = x_hud_window_tag_value;
            [hud showAnimated:true];
            hud.label.numberOfLines = 6;
        }
        hud.completionBlock = hideComplete;
        hud.mode = MBProgressHUDModeText;
        hud.label.text = tip;
        [hud hideAnimated:true afterDelay:delay];
    });
}


#pragma mark - hide
+ (void)hideLoading:(UIViewController *)viewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [viewController.view viewWithTag:x_hud_viewController_tag_value];
        if(hud) {
            [hud hideAnimated:false];
        }
    });
}

+ (void)hideLoadingOnWindow{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
        
        MBProgressHUD *hud = [window viewWithTag:x_hud_window_tag_value];
        if(hud) {
            [hud hideAnimated:false];
        }
    });
}
@end
NS_ASSUME_NONNULL_END
